<template>
    <div>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="user.userName" name="userName" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="user.password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
                <van-field v-model="user.code" center clearable label="验证码" name="code" placeholder="请输入短信验证码"
                    :rules="[{ required: true, message: '请输入验证码' }]">
                    <template #button>
                        <van-button :disabled="time === 5 ? false : true" size="small" type="primary" @click="yzm">{{ zz
                            }}</van-button>
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
const router=useRouter()
import { usedl } from '../api/api';
import { setActivePinia } from 'pinia';
import { showToast } from 'vant';
const { user } = setActivePinia(usedl())
const time = ref(5)
const zz = ref('发送验证码')
let timer = null
const yzm = () => {
    timer = setInterval(() => {
        if (time.value === 1) {
            clearInterval(timer)
            time.value = 5
            zz.value = '发送验证码'
        }
        else {
            time.value -= 1;
            zz.value = `请${time.value}秒后重试`
        }
    }, 1000)
}
const onSubmit = ({ userName, password, code }) => {
    if (userName === 'admin' && password === '123456' && code === '1234') {
        showToast('登录成功')
        router.push('/home')
    }
    else {
        showToast('登录失败')
    }
};
</script>

<style></style>